package com.dogkr.server.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dogkr.server.entity.Host;
import com.dogkr.server.entity.Order;
import com.dogkr.server.entity.Order.OrderStatus;
import com.dogkr.server.entity.OrderEvaluate;
import com.dogkr.server.entity.OrderPet;
import com.dogkr.server.entity.OrderServer;
import com.dogkr.server.entity.PetServerValuation;
import com.dogkr.server.entity.UserMessage;
import com.dogkr.server.entity.UserPet;
import com.dogkr.server.mapper.HostPetServerMapper;
import com.dogkr.server.mapper.HostPetServerRestrainMapper;
import com.dogkr.server.mapper.HostPetServerValuationMapper;
import com.dogkr.server.mapper.OrderMapper;
import com.dogkr.server.mapper.OrderPetMapper;
import com.dogkr.server.mapper.OrderServerMapper;
import com.dogkr.server.mapper.UserMessageMapper;
import com.sean.common.page.Pagination;
import com.sean.common.page.ResultPagination;
import com.sean.common.service.BaseService;
import com.sean.common.utils.StringUtils;

@Service
public class OrderService extends BaseService {
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderPetMapper orderPetMapper;
	@Autowired
	private OrderServerMapper orderServerMapper;
	@Autowired
	private HostService hostService;
	@Autowired
	private UserPetService userPetService;
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private HostPetServerMapper hostPetServerMapper;
	@Autowired
	private HostPetServerValuationMapper hostPetServerValuationMapper;
	@Autowired
	private HostPetServerRestrainMapper hostPetServerRestrainMapper;
	/**
	 * 创建预约单.
	 * @param order
	 */
	public void createOrder(Order order) {
		Host host = hostService.selectByPrimaryKey(order.getHostId());
		order.setCreateDate(new Date());
		order.setLastModifydate(order.getCreateDate());
		order.setStatus(OrderStatus.CREATE.getVal());
		order.setHostName(host.getHostName());
		order.setUserRemark(StringUtils.textareaToHtml(order.getUserRemark()));
		order.setOrderNo(getOrderNo(order.getUserId(), order.getHostId()));
		orderMapper.insert(order);
		for (OrderPet pet : order.getPetList()) {
			UserPet userPet = userPetService.selectByPrimaryKey(pet.getPetId());
			pet.setUserPet(userPet);
			pet.setOrderId(order.getId());
			orderPetMapper.insert(pet);
		}
		UserMessage userMessage = UserMessage.createOrderMsg(order, host.getUserId());
		messageService.insertMessage(userMessage, null);
	}
	
	private String getOrderNo(Long userId, Long hostId) {
		return StringUtils.getNoString(userId.toString(), 5) + StringUtils.getNoString(hostId.toString(), 5) + (new Date()).getTime()/1000;
	}
	
	public ResultPagination<Order> selectList(Pagination pagination) {
		return this.selectListForPage(OrderMapper.class, "selectByList", pagination, Order.class);
	}
	

	/**
	 * 展示页面评论列表.
	 * @param order
	 */
	public ResultPagination<OrderEvaluate> selectWithAppraise(Pagination pagination, Long hostId) {
		pagination.putParam("hostId", hostId);
		return this.selectListForPage(OrderMapper.class, "selectWithAppraise", pagination, OrderEvaluate.class);
	}
	
	
	public Order selectByPrimaryKeyWithAll(Long id) {
		return orderMapper.selectByPrimaryKeyWithAll(id);
	}

	/**
	 * 谢绝预约.
	 * @param id
	 * @param hostId
	 */
	public void rejectOrder(Long id, Long hostId) {
		Order order = orderMapper.selectByPrimaryKey(id);
		if(order != null && hostId.longValue() == order.getHostId()) {
			order.setStatus(Order.OrderStatus.REJECT.getVal());
			order.setLastModifydate(new Date());
			orderMapper.updateByPrimaryKeySelective(order);
			messageService.updateAnswerByOrderKey(order.getId());
		}
	}
	

	/**
	 * 接受预约.
	 * @param id
	 * @param hostId
	 */
	public void agreeOrder(Long id, Long hostId) {
		Order order = orderMapper.selectByPrimaryKey(id);
		if(order != null && hostId.longValue() == order.getHostId()) {
			order.setStatus(Order.OrderStatus.AGREE.getVal());
			order.setLastModifydate(new Date());
			orderMapper.updateByPrimaryKeySelective(order);
			messageService.updateAnswerByOrderKey(order.getId());
		}
	}

	public Order selectByPrimaryKey(Long id) {
		return orderMapper.selectByPrimaryKey(id);
	}

	public void createOrderServer(Order order, BigDecimal discount, List<OrderServer> serverList) throws ParseException {
		BigDecimal orderAmount = new BigDecimal(0);
		for (OrderServer orderServer : serverList) {
			PetServerValuation valuation = hostPetServerValuationMapper.selectByPrimaryKeyWithServer(orderServer.getValId());
			orderServer.createInit(order, valuation);
			orderAmount = orderAmount.add(orderServer.getServerPriceAll());
			orderServerMapper.insert(orderServer);
		}
		order.setStatus(Order.OrderStatus.QUOTATION);
		order.setOrderAmount(orderAmount);
		//计算浮动价格
		if(discount != null) orderAmount = orderAmount.add(discount);
		order.setOrderStrikePrice(orderAmount);
		
		orderMapper.updateByPrimaryKeySelective(order);
	}
	
	public void saveAppraise(Order order) {
		Order record = new Order();
		record.setId(order.getId());
		record.setEvaluate(order.getEvaluate());
		record.setAppraise(order.getAppraise());
		record.setStatus(OrderStatus.ASSESS);
		orderMapper.updateByPrimaryKeySelective(record);
	}
}
